//
//  E7OpenGLLayer.cpp
//  EndlessBattle
//
//  Created by cc on 12-3-15.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7OpenGLLayer.h"

#include "E7Logic.h"

CE7OpenGLLayer::CE7OpenGLLayer()
{
    m_fScale = 1.0f ;
}

CE7OpenGLLayer::~CE7OpenGLLayer()
{
}

void CE7OpenGLLayer::draw()
{
    if (!s_pLogic->IsActived())
    {
//        printf("open gl draw !s_pLogic->IsActived()\n") ;
        return ;
    }

    if (NULL == s_pLogic->s_pSelect)
    {
//        printf("open gl draw NULL == s_pSelect\n") ;
        return ;
    }

    if (0 != s_pLogic->s_pSelect->GetCountryID())
    {
        return ;
    }

    if (IsPad())
    {
        glLineWidth(5.0f) ;
    }
    else
    {
        glLineWidth(2.5f) ;
    }
    glScalef(m_fScale, m_fScale, m_fScale) ;
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f) ;

//    int tc = s_pSelect->color ;
//    glColor4f(HEXAGON_COLOR_ARR[tc][0], HEXAGON_COLOR_ARR[tc][1], HEXAGON_COLOR_ARR[tc][2], HEXAGON_COLOR_ARR[tc][3]) ;

//    ccDrawPoly(s_pSelect->GetBorderPoints(), 6, true, false) ;

    // draw a cell.
    if (-1 == s_pLogic->s_pSelect->GetTownID())
    {
        ccDrawPoly(s_pLogic->s_pSelect->GetBorderPoints(), 6, true, false) ;
        return ;
    }

    // find the town.
    TOWN_INFO_LIST::iterator itr ;
    CE7Town* pTownSel = NULL ;
    for (itr = s_pLogic->m_listTown.begin(); itr != s_pLogic->m_listTown.end(); ++itr)
    {
        if ((*itr)->GetID() == s_pLogic->s_pSelect->GetTownID())
        {
            pTownSel = (*itr) ;
            break ;
        }
    }

    // not find the town.
    if (NULL == pTownSel)
    {
        printf("open gl draw error : not found the town by cell id [%d] [%d] [%d]\n",
               s_pLogic->s_pSelect->GetID(), s_pLogic->s_pSelect->GetTownID(), s_pLogic->s_pSelect->GetCountryID()) ;
        return ;
    }

    // draw the town.
    CELL_INFO_LIST::iterator itr_cell ;
    CE7Cell* pCell = NULL ;

    TOWN_ID_LIST::iterator itr_id ;
//    printf("open gl draw debug info : [%lu] [%lu]\n", pTownSel->m_listCellIDs.size(), s_pLogic->ActiveMap()->m_listCells.size()) ;
    for (itr_id = pTownSel->m_listCellIDs.begin() ; itr_id != pTownSel->m_listCellIDs.end(); ++itr_id)
    {
        for (itr_cell = s_pLogic->ActiveMap()->m_listCells.begin(); itr_cell != s_pLogic->ActiveMap()->m_listCells.end(); ++itr_cell)
        {
            pCell = (*itr_cell) ;
            if ((*itr_id) == pCell->GetID())
            {
                // todo.
//                printf("debug ---- enter flag\n") ;
                for (int i = 0; i < 6; ++i)
                {
                    if (NULL == pCell->GetNeighborPointer(i))
                    {
                        ccDrawLine(pCell->GetBorders((i)%6), pCell->GetBorders((i+1)%6)) ;
                    }
                    //else if (pCell->GetCountryID() == pCell->GetNeighborPointer(i)->GetCountryID())
                    else if (pCell->GetTownID() == pCell->GetNeighborPointer(i)->GetTownID())
                    {
                        // todo.
//                        printf("draw line info - 1: [%d] [%d]\n", pCell->GetCountryID(), pCell->GetNeighborPointer(i)->GetCountryID()) ;
//                        printf("draw line info - 2: [%d] [%d]\n", pCell->GetTownID(), pCell->GetNeighborPointer(i)->GetTownID()) ;
//                        printf("draw line info - 3: [%d] [%d]\n", pCell->GetID(), pCell->GetNeighborPointer(i)->GetID()) ;
                    }
                    else
                    {
                        ccDrawLine(pCell->GetBorders((i)%6), pCell->GetBorders((i+1)%6)) ;
                    }
                }

                break ;
            }
        }
    }

//    // draw all frame.
//    int tc ;
//    for (int i = 0; i < m_nCount; ++i)
//    {
//        if (-1 == m_pCellInfos[i].GetCountryID())
//        {
//            continue ;
//        }
//
//        tc = m_pCellInfos[i].GetCountryID() ;
//
////        ccDrawPoly(m_pCellInfos[i].GetBorderPoints(), 6, false, true) ; // fill.
//        ccDrawPoly(m_pCellInfos[i].GetBorderPoints(), 6, true, false) ; // draw.
//    }
}














